package org.example.everyday;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Lc937{

    public static void main(String[] args) {
        Lc937 lc937 = new Lc937();
        String[] logs = new String[]{"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};

        lc937.reorderLogFiles(logs);
    }
    public String[] reorderLogFiles(String[] logs) {
        List<String> alpha = new ArrayList<>();

        List<String> nums = new ArrayList<>();

        for(int i = 0; i < logs.length; i++){
            String cur = logs[i];
            int pos = cur.indexOf(" ");
            char ch = cur.charAt(pos + 1);
            if('0' <= ch && ch <= '9'){
                nums.add(cur);
            }else{
                alpha.add(cur);
            }
        }

        alpha.sort(new Comparator<String>(){
            public int compare(String o1,String o2){
                int pos1 = o1.indexOf(" ");
                int pos2 = o2.indexOf(" ");
                String str1 = o1.substring(pos1 + 1);
                String str2 = o2.substring(pos2 + 1);

                int cmp = str1.compareTo(str2);
                if(cmp == 0){
                    return o1.substring(0,pos1).compareTo(o2.substring(0,pos2));
                } else {
                    return cmp;
                }
            }
        });
        String[] res = new String[logs.length];
        int index = 0;
        for(int i = 0; i < logs.length; i++){
            if(i < alpha.size()){
                res[index++] = alpha.get(i);
            }else{
                res[index++] = nums.get(i - alpha.size());
            }
        }

        return res;
    }
}

